/*
 *
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * by the @author tags. See the COPYRIGHT.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.netty.handler.codec.bayeux;

import java.util.Map;

/**
 *
 * @author daijun
 */
public class BayeuxMessageFactory {

    static private BayeuxMessageFactory instance;

    private BayeuxMessageFactory() {
    }

    static public BayeuxMessageFactory getInstance() {
        if (instance == null) {
            instance = new BayeuxMessageFactory();
        }
        return instance;
    }

    public BayeuxMessage create(Map map) {
        String channel = (String) map.get("channel");
        Object[] objs = (Object[]) map.get("supportedConnectionTypes");
        String[] supportedConnectionTypes = null;
        if (objs != null && objs.length != 0) {
            supportedConnectionTypes = new String[objs.length];
            for (int i = 0; i < objs.length; i++) {
                supportedConnectionTypes[i] = (String) objs[i];
            }
        }
       
        String clientId = (String) map.get("clientId");
        String connectionId = (String) map.get("connectionId");
        String minimumVersion = (String) map.get("minimumVersion");
        Boolean successful = (Boolean) map.get("successful");
        String version = (String) map.get("version");
        String subscription = (String) map.get("subscription");
        String error = (String) map.get("error");
        String connectionType = (String) map.get("connectionType");
        String id = (String) map.get("id");
        String timestamp = (String) map.get("timestamp");
        BayeuxExt ext = map.get("ext") == null ? null : new BayeuxExt();
        BayeuxAdvice advice = map.get("advice") == null ? null : new BayeuxAdvice();
        BayeuxData data = map.get("data") == null ? null : new BayeuxData();

        BayeuxMessage bayeux = new BayeuxMessage();
        bayeux.setChannel(channel);
        bayeux.setSupportedConnectionTypes(supportedConnectionTypes);
        bayeux.setClientId(clientId);
        bayeux.setConnectionId(connectionId);
        bayeux.setSuccessful(successful);
        bayeux.setVersion(version);
        bayeux.setMinimumVersion(minimumVersion);
        bayeux.setSubscription(subscription);
        bayeux.setError(error);
        bayeux.setConnectionType(connectionType);
        bayeux.setId(id);
        bayeux.setTimestamp(timestamp);
        bayeux.setExt(ext);
        bayeux.setAdvice(advice);
        bayeux.setData(data);

        if (HandshakeRequest.isValid(bayeux)) {
            return new HandshakeRequest(bayeux);
        } else if (ConnectRequest.isValid(bayeux)) {
            return new ConnectRequest(bayeux);
        } else if (DisconnectRequest.isValid(bayeux)) {
            return new DisconnectRequest(bayeux);
        } else {
            return bayeux;
        }
    }
}
